package com.yjhb.android.sbz.fragment.check;

import android.content.Intent;
import android.view.View;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.NoPassDetailActivity;
import com.yjhb.android.sbz.activity.TaskCheckActivity;
import com.yjhb.android.sbz.adapter.CheckListAdapter;
import com.yjhb.android.sbz.entity.BatchCheck;
import com.yjhb.android.sbz.entity.CheckResult;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.FailDetail;
import com.yjhb.android.sbz.enums.TaskStatus;
import com.yjhb.android.sbz.fragment.BaseListFragment;
import com.yjhb.android.sbz.mvp.checklist.CheckListPresenter;
import com.yjhb.android.sbz.mvp.checklist.CheckListView;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 15:42
 */
public class CheckListFragment extends BaseListFragment<CheckListPresenter, CheckListAdapter, CheckResult> implements CheckListView {
    private final String TAG = "CheckListFragment";

    private Integer state;
    private Integer taskId;
    private boolean isEdit;

    @Override
    protected CheckListAdapter getAdapter() {
        return new CheckListAdapter(getContext());
    }

    @Override
    protected CharSequence getTips() {
        if (state == null) return "";
        switch (TaskStatus.getStatus(state)) {
            case TASK_STATUS_SUBMIT:
                return "注意:未在要求时间内审核，系统将自动审核通过";
            default:
                return " ";
        }

    }

    @Override
    protected void initUI() {
        super.initUI();
        tips_tv.setTextColor(getResources().getColor(R.color.red_3b));
        recyclerView.setOnAdapterDataUpdate(new EmptyRecyclerView.OnAdapterDataUpdate() {
            @Override
            public void onAdapterDataChanged() {
                if (isEdit) {
                    EventBus.getDefault().post(new EventMsg(EventMsg.TASK_CHECK_SELECT_UPDATE, null));
                }
            }

            @Override
            public void onAdapterDataItemRangeInserted(int positionStart, int itemCount) {
                if (isEdit) {
                    EventBus.getDefault().post(new EventMsg(EventMsg.TASK_CHECK_SELECT_UPDATE, null));
                }
            }

            @Override
            public void onAdapterDataItemRangeRemoved(int positionStart, int itemCount) {
                if (isEdit) {
                    EventBus.getDefault().post(new EventMsg(EventMsg.TASK_CHECK_SELECT_UPDATE, null));
                }
            }

            @Override
            public void onAdapterDataItemChanged(int positionStart, int itemCount) {
                if (isEdit) {
                    EventBus.getDefault().post(new EventMsg(EventMsg.TASK_CHECK_SELECT_UPDATE, null));
                }
            }
        });
    }


    public void setEdit(boolean edit) {
        isEdit = edit;
        if (adapter != null) {
            adapter.setEdit(isEdit);
            adapter.notifyDataSetChanged();
        }
    }

    public void selectedAll(boolean b) {
        if (isEdit && adapter != null) {
            adapter.selectedAll(b);
        }
    }

    public void check(int index) {
        if (isEdit && adapter != null) {
            adapter.check(index);
        }
    }

    public Map<Long, CheckResult> getSelectedMap() {
        if (adapter != null)
            return adapter.getSelectedMap();
        return null;
    }

    public int getCount() {
        if (adapter != null)
            return adapter.getItemCount();
        return 0;
    }

    public void submit() {
        Map<Long, CheckResult> resultMap = getSelectedMap();
        if (resultMap == null || resultMap.size() < 1) {
            ZxToast.showINFOBottomToast("请先选择您要通过的选项");
            return;
        }
        ArrayList<Integer> ids = new ArrayList<>();
        for (Long l : resultMap.keySet()) {
            ids.add(l.intValue());
        }
        BatchCheck batchCheck = new BatchCheck();
        batchCheck.setIds(ids);
        batchCheck.setReject(false);
        batchCheck.setReason(null);
        presenter.batchConfirm(batchCheck);
    }


    public void setState(Integer state) {
        this.state = state;
    }

    public void setTaskId(Integer taskId) {
        this.taskId = taskId;
    }

    @Override
    protected void onLoadData(int size, int current) {
        presenter.getCheckList(taskId + "", state, size, current);
    }

    @Override
    protected CheckListPresenter initPresenter() {
        return new CheckListPresenter(this);
    }

    @Override
    public void onItemClick(int index, Object o) {
//        if (state == 0) {
        if (isEdit) {
            adapter.check(index);
        } else {
            Intent intent = new Intent(getContext(), TaskCheckActivity.class);
            intent.putExtra("dataSet", dataSet);
            intent.putExtra("index", index);
            startActivity(intent);
        }
//        }
    }

    @Override
    public void onViewClick(View view, String msg, int index, Object o) {
        if (msg.equals("failDetails")){
            FailDetail failDetail = new FailDetail();
            failDetail.setRejectReason(dataSet.get(index).getTaskOrder().getRejectReason());
            failDetail.setRejectReasonImg(dataSet.get(index).getTaskOrder().getRejectImgs());
            Intent intent = new Intent(getContext(), NoPassDetailActivity.class);
            intent.putExtra("failDetail", failDetail);
            startActivity(intent);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (loadingDialog != null) {
            current = 1;
            onLoadData(size, current);
        }
    }

    @Override
    public void onEventMsg(EventMsg msg) {
        super.onEventMsg(msg);
        if (msg.msg == EventMsg.REQUEST_UPDATE_CHECK_LIST) {
            onLoadData(size, current);
        }
    }

    @Override
    public void onLoad(List<CheckResult> data) {
        smartRefreshLayout.finishLoadmore();
        dataSet.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(List<CheckResult> data) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        dataSet.clear();
        dataSet.addAll(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onBatchCheckSuccess(Integer count) {
        loadingDialog.cancel();
        current = 1;
        onLoadData(size,current );
        //发送消息通知activity处理已完成
        EventBus.getDefault().post(new EventMsg(EventMsg.TASK_CHECK_BATCH_SUCCESS, null));
        //发送消息通知外层列表数据需要更新
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_UPDATE_DATA_TASK_MANAGER_LIST_ITEM, null));
        if (count != null)
            ZxToast.showINFOBottomToast("处理完成，成功" + count + "条！");
    }
}
